#include "monster.h"


Monster::Monster() : MovingUnit()
{
    init();
}

Monster::Monster(QRect &geometry, int intelligence) : MovingUnit(geometry)
{
    m_intelligence = intelligence;
    init();
}


int Monster::getType() const {return Unit::Monster;}


void Monster::init()
{
    m_leftSprite = "e" + QString::number(m_intelligence) + "left";
    m_upSprite = "e" + QString::number(m_intelligence) + "up";
    m_rightSprite = "e" + QString::number(m_intelligence) + "right";
    m_downSprite = "e" + QString::number(m_intelligence) + "down";

    loadPixmaps();

    m_iaStep = 0;
}



void Monster::draw(QPainter *painter)
{
    /*
    if(m_intelligence == 1)
    {
        painter->fillRect(this->m_geometry, QColor(255,255,0));
        painter->setPen(Qt::black);
        painter->drawText(m_geometry.center().x(), m_geometry.center().y(), "M1");
    }
    else if(m_intelligence == 2)
    {
        painter->fillRect(this->m_geometry, QColor(255,204,0));
        painter->setPen(Qt::black);
        painter->drawText(m_geometry.center().x(), m_geometry.center().y(), "M2");
    }
    else if(m_intelligence == 3)
    {
        painter->fillRect(this->m_geometry, QColor(255,153,204));
        painter->setPen(Qt::black);
        painter->drawText(m_geometry.center().x(), m_geometry.center().y(), "M3");
    }
    */

    painter->drawPixmap(m_geometry, m_pixmaps[m_direction][m_step]);
}

void Monster::advance(MovingUnit *player)
{

    if (m_intelligence == 1)
    {
        switch (qrand() % 10 )
        {
            case 0 : moveLeft(); break;
            case 1 : moveUp(); break;
            case 2 : moveDown(); break;
            case 3 :
            case 4 :
            case 5 : moveRight(); break;
            case 6 :
            case 7 :
            case 8 :
            default : break;
        }
    }
    else if (m_intelligence == 2)
    {
        if (m_iaStep == 0)
        {
            if(player->getGeometry().x() > this->getGeometry().x())
                moveRight();
            else if(player->getGeometry().x() < this->getGeometry().x())
                moveLeft();
        }
        else if (m_iaStep == 1)
        {
            if(player->getGeometry().y() > this->getGeometry().y())
                moveDown();
            else if(player->getGeometry().y() < this->getGeometry().y())
                moveUp();
        }
    }

    m_iaStep = (m_iaStep +1) %2;
}


void Monster::serializeDebug(QDebug &dbg)
{
    dbg << "Monster ("
        << "Niveau:" << m_intelligence
        << "Etape de l'IA:" << m_iaStep;
    MovingUnit::serializeDebug(dbg);
    dbg << ")";
}

void Monster::serializeIn(QDataStream &in)
{
    MovingUnit::serializeIn(in);
    in >> m_intelligence;
    init();
}

void Monster::serializeOut(QDataStream &out)
{
    MovingUnit::serializeOut(out);
    out << (qint32)m_intelligence;
}
